﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Simhopp.Model;

namespace Simhopp.GUI
{
    public partial class frmCoordinator : Form, InterfaceCoordinatorView
    {
        //public static bool databaseConnection;
        public event DelegateCreateEvent EventCreate = null;
        public event DelegateGetCompetitionList EventGetCompetitionList = null;
        public event DelegateManageEvent EventManageEvent = null;
        public event DelegateEditJudges EventEditJudges = null;
        public event DelegateEditDiver EventEditDiver = null;
        public event DelegateInitializeCompetitionList EventInitializeCompetitionList = null;
        public event DelegateStartEvent EventStartEvent = null;
        public event DelegateCompetitionList EventCompetitionList = null;
        //public event PropertyChangedEventHandler PropertyChanged = null;
        public Competition PropCompetition { get; set; }
        public frmCoordinator()
        {
            InitializeComponent();
            //if (EventInitializeCompetitionList != null)
            //{
            //    CompetitionList compList = EventInitializeCompetitionList();
            //    BindingSource bindingSource = null;
            //    bindingSource.DataSource = compList;
            //    cmbCompetitions.DataSource = bindingSource.DataSource;
            //    cmbCompetitions.DisplayMember = "Name";
            //}
        }

        private void btnJudges_Click(object sender, EventArgs e)
        {
            //frmEditJudges editJudges = new frmEditJudges();
            ////if (PropertyChanged != null)
            ////    PropertyChanged(this, new PropertyChangedEventArgs("PropCompetition"));
            //editJudges.Show();
            if (EventEditJudges != null)
            {
                EventEditJudges((Competition)cmbCompetitions.SelectedItem);
                //this.Hide();
            }
        }

        private void btnCreateEvent_Click(object sender, EventArgs e)
        {
            if (EventCreate != null && EventGetCompetitionList != null)
            {
                EventCreate(EventGetCompetitionList());
            }
            
        }

        private void cmbCompetitions_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                PropCompetition = (Competition)cmbCompetitions.SelectedItem;
                SetCompetitionInfoToListBox();
            }
            catch
            {

            }
        }

        private void SetCompetitionInfoToListBox()
        {
            lstCompetitionInfo.Items.Clear();
            lstCompetitionInfo.Items.Add(PropCompetition.Name);
            lstCompetitionInfo.Items.Add(PropCompetition.Location);
            lstCompetitionInfo.Items.Add(PropCompetition.Date);
        }

        private void frmCoordinator_Load(object sender, EventArgs e)
        {
            //cmbCompetitions.SelectedIndex = 0;
            if (EventInitializeCompetitionList != null)
            {
                try
                {
                    CompetitionList compList = EventInitializeCompetitionList();
                    BindingSource bindingSource = new BindingSource();
                    bindingSource.DataSource = compList.competitionList;
                    cmbCompetitions.DataSource = bindingSource.DataSource;
                    cmbCompetitions.DisplayMember = compList.competitionList.ToString();
                    frmLogin.databaseConnection = true;
                    tsslDataConnect.Text = "Connected";
                }
                catch (Exception exception)
                {
                    if (frmLogin.databaseConnection == true)
                    {
                        frmLogin.databaseConnection = false;
                        MessageBox.Show(exception.ToString(), "Fel", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    tsslDataConnect.Text = "Not Connected";
                }
            }
        }

        private void btnDivers_Click(object sender, EventArgs e)
        {
            if (cmbCompetitions.SelectedItem != null)
            {
                if (EventEditDiver != null)
                    EventEditDiver(PropCompetition);
            }
            
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (cmbCompetitions.SelectedItem != null && EventStartEvent != null)
                EventStartEvent(PropCompetition);
        }

        private void btnEditCompetition_Click(object sender, EventArgs e)
        {
            if (EventManageEvent != null && EventGetCompetitionList != null)
            {
                EventManageEvent(EventGetCompetitionList(), PropCompetition);
            }
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                if (EventInitializeCompetitionList != null)
                {
                    List<Competition> compList = EventCompetitionList();
                    cmbCompetitions.DataSource = null;
                    cmbCompetitions.DataSource = compList;
                    cmbCompetitions.SelectedIndex = compList.Count - 1;
                   
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
    }
}
